from typing import Union

from pydantic import ValidationError

from common.env import ENV

'''
这里统一处理一下错误函数的逻辑
'''


'''
e.errors() -> 返回list

list返回格式举例如下：
[{
'type': 'model_type', 
'loc': ('data',), 
'msg': 'Input should be a valid dictionary or instance of BaseModel', 
'input': [{'organization_id': 2, 'name': 'fintech'}, {'organization_id': 1, 'name': 'zjlab'}], 'ctx': {'class_name': 'BaseModel'}, 
'url': 'https://errors.pydantic.dev/2.9/v/model_type'}]
'''
def error_pydantic_msg(e: ValidationError) -> Union[list, dict, str, None]:
    return {
        "title": repr(e).split('\n')[0],    # validation错误问题 概要解释
        "details": e.errors(include_url=False) if ENV == "prod"  else e.errors()   
    }
    
